package com.shsubway.base;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.shsubway.R;
import com.shsubway.utils.Constant;
import com.shsubway.utils.SPUtils;

/**
 * Created by Hu on 2017/2/27.
 */

public class BaseActivity extends AppCompatActivity {
    public Context context;
    public View emptyView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("BaseActivity", "BaseActivity");
        context = this;
        emptyView=View.inflate(context, R.layout.empty_view,null);
    }

    public void setTitleBar(Toolbar toolbar, String title) {
        toolbar.setTitle(title.trim());
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    public void setTitleBarNoBack(Toolbar toolbar, String title) {
        toolbar.setTitle(title.trim());
        setSupportActionBar(toolbar);

    }

    public void setRefresh(SwipeRefreshLayout myRefreshLayout) {
        myRefreshLayout.setColorSchemeResources(
                android.R.color.holo_blue_light, android.R.color.holo_red_light,
                android.R.color.holo_orange_light, android.R.color.holo_green_light);
    }

    public <T> T $(int viewID, View heardView) {
        return (T) heardView.findViewById(viewID);
    }

    public void setRecy(RecyclerView recy) {

        recy.setHasFixedSize(true);
        recy.setLayoutManager(new LinearLayoutManager(context));
        recy.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    }

    public void setGRecy(RecyclerView recy, int i) {

        recy.setHasFixedSize(true);

        recy.setLayoutManager(new GridLayoutManager(context, i));

    }

    public void startActivity(Class<?> cls) {
        Intent intent = new Intent();
        intent.putExtra(Constant.TAG, context.getClass().getSimpleName());
        intent.setClass(context, cls);
        startActivity(intent);
    }

    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.putExtras(bundle);
        intent.putExtra(Constant.TAG, context.getClass().getSimpleName());
        intent.setClass(context, cls);
        startActivity(intent);
    }

    public void startActivity(Class<?> cls, Intent intent) {
        intent.setClass(context, cls);
        intent.putExtra(Constant.TAG, context.getClass().getSimpleName());
        startActivity(intent);
    }

    public void toast(Object o) {
        if (o instanceof Integer) {
            Toast.makeText(context, (Integer) o, Toast.LENGTH_SHORT).show();
        } else if (o instanceof CharSequence) {
            Toast.makeText(context, (CharSequence) o, Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(context, o + "", Toast.LENGTH_SHORT).show();
        }

    }


    public void put(String key, Object object) {
        SPUtils.put(context, key, object);
    }

    public Object get(String key, Object object) {
        return SPUtils.get(context, key, object);
    }
}
